package com.jerei.store.controller;

import com.jerei.store.entity.Address;
import com.jerei.store.entity.District;
import com.jerei.store.service.IAddressService;
import com.jerei.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("address")
public class AddressController extends BaseController {

    @Autowired
    private IAddressService addressService;

    @RequestMapping("add")
    public JsonResult<Void> addNewAddress(Address address, HttpSession session){
        Integer uid = getUidFromSession(session);
        String username=getUsernameFromSession(session);
        addressService.addAddress(address,uid,username);
        return new JsonResult<>(OK);
    }


    @RequestMapping("find/province")
    public JsonResult<List<District>> findAllProvince(){
        List<District> list=addressService.findAllProvince();
        return new JsonResult<>(OK,list);
    }
    @RequestMapping("find/city")
    public JsonResult<List<District>> findAllCity(Integer val){
        System.err.println("传过来了"+val);
        List<District> list=addressService.findAllCity(val);
        return new JsonResult<>(OK,list);
    }

    @RequestMapping("find/area")
    public JsonResult<List<District>> findAllArea(Integer val){
        System.err.println("传过来了"+val);
        List<District> list=addressService.findAllArea(val);
        return new JsonResult<>(OK,list);
    }


    @RequestMapping("findAddress")
    public JsonResult<List<Address>> findAllByUid(HttpSession session){

        Integer uid=Integer.valueOf(session.getAttribute("uid").toString()) ;
        List<Address> list=addressService.findAllByUid(uid);
        return new JsonResult<>(OK,list);
    }

    @RequestMapping("delete")
    public JsonResult<Void> deleteAddress(Integer aid){

        addressService.deleteAddress(aid);
        return new JsonResult<>(OK);

    }

    @RequestMapping("setDefault")
    public JsonResult<Void> setDefault(Integer aid,HttpSession session){
        Integer uid=getUidFromSession(session);
        String username=getUsernameFromSession(session);
        addressService.setDefault(aid,uid,username);
        return new JsonResult<>(OK);
    }




}
